package com.tenglong.teacher.ui.adapter.viewholder;

import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClassInfo;
import com.tenglong.teacher.data.resultInfo.ClassInfoBean;
import com.tenglong.teacher.data.resultInfo.SeClass;
import com.tenglong.teacher.ui.adapter.HomePagerAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;

/**
 * @author hdx on 2019/4/18.
 */
public class HomePagerViewHolder extends BaseViewHolder<ClassInfoBean> {
    private TextView className, classCode, ivHeader, teacherName, studentNum;

    private HomePagerAdapter.HomePagerListener listener;

    public HomePagerViewHolder(View itemView) {
        super(itemView);
        initView();
    }

    public HomePagerViewHolder(View itemView, HomePagerAdapter.HomePagerListener listener) {
        this(itemView);
        this.listener = listener;
        setListeners();
    }

    private void setListeners() {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onHomePagerListener(getLayoutPosition());
                }
            }
        });
    }

    private void initView() {
        className = itemView.findViewById(R.id.tv_className);
        classCode = itemView.findViewById(R.id.classCode);
        ivHeader = itemView.findViewById(R.id.iv_header);
        teacherName = itemView.findViewById(R.id.tv_teacher_name);
        studentNum = itemView.findViewById(R.id.tv_stu_num);
    }

    @Override
    public void setLayoutInfo(ClassInfoBean classInfoBean) {
        SeClass bean= (SeClass) classInfoBean;
        if (bean.isInClass()) {
            String name = bean.getName();
            if (name == null) {
                name = "";
            }
            SpannableStringBuilder builder = new SpannableStringBuilder(name + "（" + "上课中" + "）");
            builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.app_red)), name.length(), name.length() + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            className.setText(builder);
        } else {
            className.setText(bean.getName());
        }
        classCode.setVisibility(TextUtils.isEmpty(bean.getClassno()) ? View.GONE : View.VISIBLE);
        if (classCode.getVisibility() == View.VISIBLE) {
            classCode.setText("班级ID：" + bean.getClassno());
        }
        teacherName.setText(bean.getDirectorName().equals("") ? "暂无" : bean.getDirectorName());
        studentNum.setText(bean.getStudentCount() + "人");
        if (bean.getDirectorName() != null) {
            int len = bean.getDirectorName().length();
            if (len > 2) {
                ivHeader.setText(bean.getDirectorName().substring(len - 2, len));
            } else {
                if (len == 0) {
                    ivHeader.setText("暂无");
                } else {
                    ivHeader.setText(bean.getDirectorName());
                }
            }
        } else {
            ivHeader.setText("暂无");
        }
    }
}
